"""
登录页面对象
"""

import allure
from selenium.webdriver.common.by import By

from base.base_page import BasePage, BaseHandler, BaseProxy
from page.home_page import HomeProxy
from page.me_page import MeProxy

class LoginPage(BasePage):
    def __init__(self):
        super().__init__()
        self.phone_field = By.ID, 'com.bjcsxq.chat.carfriend:id/login_phone_et'
        self.password_field = By.ID, 'com.bjcsxq.chat.carfriend:id/login_pwd_et'
        self.login_btn = By.ID, 'com.bjcsxq.chat.carfriend:id/login_btn'
        self.toast_message = By.ID, 'com.bjcsxq.chat.carfriend:id/txt_msg'
        self.toast_check_btn = By.XPATH, '//*[@text="确定"]'
        self.hello_text = By.XPATH, '//*[@text="您好！请登录"]'

    def find_phone_field(self):
        return self.get_element(self.phone_field)

    def find_password_field(self):
        return self.get_element(self.password_field)

    def find_login_button(self):
        return self.get_element(self.login_btn)

    def find_toast_message(self):
        return self.get_element(self.toast_message)

    def find_toast_check_button(self):
        return self.get_element(self.toast_check_btn)



class LoginHandler(BaseHandler):
    def __init__(self):
        super().__init__()
        self.login_page = LoginPage()

    @allure.step(title='输入手机号')
    def input_phone_number(self, phone_number):
        self.input_text(self.login_page.find_phone_field(), phone_number)

    @allure.step(title='输入密码')
    def input_password(self, password):
        self.input_text(self.login_page.find_password_field(), password)

    @allure.step(title='点击登录按钮')
    def click_login_btn(self):
        self.login_page.find_login_button().click()

    @allure.step(title='获取弹窗提示消息')
    def get_toast_message(self):
        return self.login_page.find_toast_message().text

    @allure.step(title='点击弹窗确定按钮')
    def click_toast_check_button(self):
        self.login_page.find_toast_check_button().click()


class LoginProxy(BaseProxy):

    def __init__(self):
        super().__init__()
        self.login_handle = LoginHandler()

    @allure.step(title='流程:登录')
    def login(self, phone_number, password):
        self.login_handle.input_phone_number(phone_number)
        self.login_handle.input_password(password)
        self.login_handle.click_login_btn()

    @allure.step(title='流程:登录成功并获取弹窗消息')
    def login_and_check_message(self, phone_number, password):
        """
        登录后检查是否登录成功
        :return:登录后的弹窗消息
        """
        self.login(phone_number, password)
        return self.login_handle.get_toast_message()

    @allure.step(title='流程:点击弹窗关闭按钮')
    def click_close_toast(self):
        """
        点击关闭弹窗按钮
        :return:
        """
        self.login_handle.click_toast_check_button()

    @allure.step(title='流程:判断是否还在登录页面')
    def is_in_login_page(self):
        """
        判断是否还在登录页面
        :return:
        """
        return self.is_element_exist(self.login_handle.login_page.hello_text)

if __name__ == '__main__':
    HomeProxy().go_me_page()
    MeProxy().go_login_page()
    print(LoginProxy().is_in_login_page())
    print(LoginProxy().login_and_check_message('18800000001', '123456'))
    LoginProxy().click_close_toast()
    print(LoginProxy().is_in_login_page())
    print(LoginProxy().login_handle.login_page.login_btn)